package org.little.test.controller;

import org.little.test.model.Books;
import org.little.test.repository.BooksRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
//import org.apache.commons.lang.StringUtils;

@RestController
public class BooksRestController {

    @Autowired
    private BooksRepository booksRepository;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    private static final Logger LOG = LoggerFactory.getLogger(BooksRestController.class);


    @RequestMapping(value = "/books/queryByParams", method = RequestMethod.GET)
    public Object queryTvsByParams(HttpServletRequest request) throws ParseException {
        try {
            String limit = request.getParameter("limit");
            String sort = request.getParameter("sort");
            String order = request.getParameter("order");
            String offset = request.getParameter("offset");

            String bookEditor = request.getParameter("bookEditor");
            String press = request.getParameter("press");
            String price = request.getParameter("price");
            String authorAbout = request.getParameter("authorAbout");
            String outline = request.getParameter("outline");
            String contentIntroduction = request.getParameter("contentIntroduction");
            String bookName = request.getParameter("bookName");


//            String time_local_start = request.getParameter("time_local_start");
//            String time_local_end = request.getParameter("time_local_end");

//            LOG.info("limit={}", limit);
//            LOG.info("sort={}", sort);
//            LOG.info("order={}", order);
//            LOG.info("offset={}", offset);
//
//            LOG.info("bookEditor={}", bookEditor);
//            LOG.info("press={}", press);
//            LOG.info("price={}", price);
//            LOG.info("authorAbout={}", authorAbout);
//            LOG.info("outline={}", outline);
//            LOG.info("contentIntroduction={}", contentIntroduction);

            String _qyeryCount = "SELECT count(*) as count FROM books where 1=1";
            String _querySql = " SELECT * FROM books where 1=1";
            String querySQl = "";
            String limitsql = String.format(" limit %s,%s", offset, limit);
            String orderSql = String.format(" order by %s %s",underscoreName(sort),order);

            if (!StringUtils.isEmpty(bookName)) {
                querySQl = querySQl + " and book_name like '%" + bookName + "%'";
            }

            if (!StringUtils.isEmpty(bookEditor)) {
                querySQl = querySQl + " and book_editor like '%" + bookEditor + "%'";
            }

            if (!StringUtils.isEmpty(press)) {
                querySQl = querySQl + " and press like '%" + press + "%'";
            }

            if (!StringUtils.isEmpty(price)) {
                querySQl = querySQl + " and price =" + price;
            }

            if (!StringUtils.isEmpty(authorAbout)) {
                querySQl = querySQl + " and price like '%" + authorAbout + "%'";
            }

            if (!StringUtils.isEmpty(outline)) {
                querySQl = querySQl + " and outline like '%" + outline + "%'";
            }

            if (!StringUtils.isEmpty(contentIntroduction)) {
                querySQl = querySQl + " and contentIntroduction like '%" + contentIntroduction + "%'";
            }


            List<Books> books = new ArrayList<Books>();

            List<Map<String, Object>> countresurlt = jdbcTemplate.queryForList(_qyeryCount + querySQl);
            long count = (long) countresurlt.get(0).get("count");
            if (count > 0) {
                RowMapper<Books> rowMapper = new BeanPropertyRowMapper<Books>(Books.class);
                books = jdbcTemplate.query(_querySql + querySQl + orderSql + limitsql, rowMapper);
            }


            Map res = new HashMap();
            res.put("rows", books);
            res.put("total", count);
            return res;
        } catch (Exception e) {
            e.printStackTrace();
            Map res = new HashMap();
            res.put("rows", new ArrayList<Map>());
            res.put("total", 0);
            return res;
        }
    }

    public Long StrDataToLong2(String dataStr) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = null;
        Long resutlt = 0L;
        try {
            date = format.parse(dataStr);
            resutlt = date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return resutlt;
    }

    public String underscoreName(String name) {
        StringBuilder result = new StringBuilder();
        if ((name != null) && (name.length() > 0)) {
            result.append(name.substring(0, 1).toUpperCase());

            for (int i = 1; i < name.length(); i++) {
                String s = name.substring(i, i + 1);

                if ((s.equals(s.toUpperCase())) && (!Character.isDigit(s.charAt(0)))) {
                    result.append("_");
                }

                result.append(s.toUpperCase());
            }
        }
        return result.toString();
    }
}



